package recipese.service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import recipese.bean.Ingredient;
import recipese.bean.Recipe;
import recipese.bean.ShareRecipe;
import recipese.dao.IngredientDao;
import recipese.dao.RecipeDao;
import recipese.dao.ShareRecipeDao;


public class RecipeService {

	private RecipeDao recipeDao;
	
	private IngredientDao ingredientDao;
	
	private ShareRecipeDao shareRecipeDao;

	public void setRecipeDao(RecipeDao recipeDao) {
		this.recipeDao = recipeDao;
	}

	public void setIngredientDao(IngredientDao ingredientDao) {
		this.ingredientDao = ingredientDao;
	}
	
	public void setShareRecipeDao(ShareRecipeDao shareRecipeDao) {
		this.shareRecipeDao = shareRecipeDao;
	}
	
	public void saveIngredient(Ingredient ingredient) {
		ingredientDao.create(ingredient);		
	}
	
	public void saveShare(ShareRecipe share) {
		shareRecipeDao.create(share);	
	}
	
	public void saveRecipe(Recipe recipe) {

		if (recipe.getId() != null) {
			recipeDao.update(recipe);
		} else {
			recipeDao.create(recipe);
		}

	}
	
	public void deleteIngredientsByRecipeId(String recipeId) {
		ingredientDao.deleteByRecipeId(recipeId);
	}
	
	public void deleteSharesByRecipeId(String recipeId) {
		shareRecipeDao.deleteByRecipeId(recipeId);
	}
	
	public void deleteRecipe(String id) {
		recipeDao.deleteById(id);
	}

	public Recipe getRecipeById(String id) {
		return recipeDao.getById(id);
	}
	
	public List<Recipe> getAllRecipes(String userId) {
		return recipeDao.getAll(userId);
	}
	
	public List<Recipe> getAllRecipes(Map<String, Boolean> sortProperties, String userId) {
		return recipeDao.getAll(sortProperties, userId);
	}
	
	public List<Recipe> loadIngredients(List<Recipe> recipes) {
		for (Recipe recipe : recipes) {
			Map<String,Object> properties = new LinkedHashMap<String,Object>();
			properties.put("recipeId", recipe.getId());
			recipe.getIngredients().addAll(ingredientDao.findByProperties(properties));
		}
		return recipes;
	}
	
	public Recipe loadIngredients(Recipe recipe) {
		Map<String,Object> properties = new LinkedHashMap<String,Object>();
		properties.put("recipeId", recipe.getId());
		recipe.getIngredients().addAll(ingredientDao.findByProperties(properties));
		return recipe;
	}
	
	public List<Recipe> loadShares(List<Recipe> recipes) {
		for (Recipe recipe : recipes) {
			Map<String,Object> properties = new LinkedHashMap<String,Object>();
			properties.put("recipeId", recipe.getId());
			recipe.getShares().addAll(shareRecipeDao.findByProperties(properties));
		}
		return recipes;
	}
	
	public Recipe loadShares(Recipe recipe) {
		Map<String,Object> properties = new LinkedHashMap<String,Object>();
		properties.put("recipeId", recipe.getId());
		recipe.getShares().addAll(shareRecipeDao.findByProperties(properties));
		return recipe;
	}

	//TODO support sorting here
	public List<ShareRecipe> getAllShareRecipes(Map<String, Boolean> sortProperties, String userId) {
		Map<String, Object> properties = new LinkedHashMap<String, Object>();
		properties.put("userId", userId);
		List<ShareRecipe> shares = shareRecipeDao.findByProperties(properties);
		return shares;
	}
	
	public List<ShareRecipe> loadRecipes(List<ShareRecipe> shares) {
		for (ShareRecipe share : shares) {
			share.setRecipe(recipeDao.getById(share.getRecipeId()));
		}
		return shares;
	}
	
	public ShareRecipe loadRecipe(ShareRecipe share) {
		share.setRecipe(recipeDao.getById(share.getRecipeId()));

		return share;
	}
}
